//
//  LXUserInfoViewController.m
//  cat
//
//  Created by imera on 16/3/14.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXUserInfoViewController.h"
#import "LXNavBarView.h"
#import "LXMineCellView.h"
#import "LXEditViewController.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD+MJ.h"
#import "AFNetworking.h"
#import "LXVideo.h"
#import "LXUpLoadVideoViewController.h"

@interface LXUserInfoViewController() <LXNavBarViewDelegate>

@property (nonatomic, weak) UILabel *userNameLabel;

@property (nonatomic, weak) UIButton *editBtn;

@property (nonatomic, strong) NSArray *videoArr;

@end

@implementation LXUserInfoViewController

- (void)setIsEdit:(BOOL)isEdit
{
    _isEdit = isEdit;
    self.editBtn.hidden = !isEdit;
}

- (void)setUser:(LXUser *)user
{
    if (user != nil) {
        _user = user;
    }
    [self initSubViews];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = MainBackgroundColor;
    
    self.isEdit = YES;
    
    [self initSubViews];
    [self addObserver];

}

#pragma mark - 注册通知
- (void)addObserver
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userChange) name:@"userNameChange" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userChange) name:@"userSexChange" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userChange) name:@"userInfoChange" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userChange) name:@"userHeadPicChange" object:nil];
}

#pragma mark - 用户昵称被修改
- (void)userChange
{
    [self initSubViews];
}

#pragma mark - 初始化子控件
- (void)initSubViews
{
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.leftTitle = @"个人资料";
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, 40);
    navBarView.myDelegate = self;
    [self.view addSubview:navBarView];
    
    /** 用户背景图片 */
    UIImageView *bgImageView = [[UIImageView alloc]init];
    bgImageView.userInteractionEnabled = YES;
    bgImageView.image = [UIImage imageNamed:@"83"];
    [self.view addSubview:bgImageView];
    
    CGFloat bgImageViewX = 0;
    CGFloat bgImageViewY = 60;
    CGFloat bgImageViewW = self.view.frame.size.width;
    CGFloat bgImageViewH = self.view.frame.size.height * 0.3;
    bgImageView.frame = CGRectMake(bgImageViewX, bgImageViewY, bgImageViewW, bgImageViewH);
    
    /** 用户头像 */
    UIImageView *userPhoto = [[UIImageView alloc]init];
    [userPhoto sd_setImageWithURL:[NSURL URLWithString:self.user.headpic]];
    [bgImageView addSubview:userPhoto];
    
    CGFloat userPhotoX = 10;
    CGFloat userPhotoY = 60;
    CGFloat userPhotoW = 80;
    CGFloat userPhotoH = userPhotoW;
    userPhoto.frame = CGRectMake(userPhotoX, userPhotoY, userPhotoW, userPhotoH);
    
    //设置圆角
    userPhoto.layer.masksToBounds = YES;
    userPhoto.layer.cornerRadius = userPhotoW * 0.5;
    userPhoto.layer.borderWidth = 2;
    userPhoto.layer.borderColor = [UIColor whiteColor].CGColor;
    
    /** 用户姓名 */
    UILabel *userNameLabel = [[UILabel alloc]init];
    userNameLabel.text = [self.user.uname isEqualToString:@""] ? self.user.email : self.user.uname;
    userNameLabel.textColor = [UIColor whiteColor];
    userNameLabel.font = [UIFont systemFontOfSize:18];
    [bgImageView addSubview:userNameLabel];
    self.userNameLabel = userNameLabel;
    
    CGSize userNameLabelSize = [userNameLabel.text sizeWithAttributes:@{NSFontAttributeName:userNameLabel.font}];
    CGFloat userNameLabelX = CGRectGetMaxX(userPhoto.frame) + 10;
    CGFloat userNameLabelY = userPhotoY + 5;
    CGFloat userNameLabelW = userNameLabelSize.width;
    CGFloat userNameLabelH = userNameLabelSize.height;
    userNameLabel.frame = CGRectMake(userNameLabelX, userNameLabelY, userNameLabelW, userNameLabelH);
    
    /** 用户等级 */
    UIImageView *userLevelImageView = [[UIImageView alloc]init];
    NSString *userLevel = [NSString stringWithFormat:@"vip_icon%ld",self.user.ulevel];
    userLevelImageView.image = [UIImage imageNamed:userLevel];
    [bgImageView addSubview:userLevelImageView];
    
    CGFloat userLevelImageViewX = CGRectGetMaxX(userNameLabel.frame) + 5;
    CGFloat userLevelImageViewY = userNameLabelY + 2;
    CGFloat userLevelImageViewW = 18;
    CGFloat userLevelImageViewH = 18;
    userLevelImageView.frame = CGRectMake(userLevelImageViewX, userLevelImageViewY, userLevelImageViewW, userLevelImageViewH);
    
    /** 用户性别 */
    UIImageView *userSexImageView = [[UIImageView alloc]init];
    if (self.user.sex == 0) {
        userSexImageView.image = [UIImage imageNamed:@"man"];
    }else{
        userSexImageView.image = [UIImage imageNamed:@"women"];
    }

    [bgImageView addSubview:userSexImageView];
    
    CGFloat userSexImageViewX = CGRectGetMaxX(userLevelImageView.frame) + 5;
    CGFloat userSexImageViewY = userNameLabelY + 1;
    CGFloat userSexImageViewW = 21;
    CGFloat userSexImageViewH = 21;
    userSexImageView.frame = CGRectMake(userSexImageViewX, userSexImageViewY, userSexImageViewW, userSexImageViewH);
    
    /** 关注的人和粉丝 */
    UILabel *concernAndFansLabel = [[UILabel alloc]init];
    NSString *concernAndFans = [NSString stringWithFormat:@"关注的人:%ld | 粉丝:%ld",self.user.fans,self.user.follow];
    concernAndFansLabel.text = concernAndFans;
    concernAndFansLabel.textColor = [UIColor whiteColor];
    [bgImageView addSubview:concernAndFansLabel];
    
    CGSize concernAndFansLabelSize = [concernAndFansLabel.text sizeWithAttributes:@{NSFontAttributeName:concernAndFansLabel.font}];
    CGFloat concernAndFansLabelX = userNameLabelX;
    CGFloat concernAndFansLabelY = CGRectGetMaxY(userSexImageView.frame) + 20;
    CGFloat concernAndFansLabelW = concernAndFansLabelSize.width;
    CGFloat concernAndFansLabelH = concernAndFansLabelSize.height;
    concernAndFansLabel.frame = CGRectMake(concernAndFansLabelX, concernAndFansLabelY, concernAndFansLabelW, concernAndFansLabelH);
    
    /** 编辑 */
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editBtn addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
    editBtn.layer.masksToBounds = YES;
    editBtn.layer.cornerRadius = 5;     //圆角
    editBtn.layer.borderWidth = 2;
    editBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    [bgImageView addSubview:editBtn];
    self.editBtn = editBtn;
    
    CGFloat editBtnX = userPhotoX;
    CGFloat editBtnY = CGRectGetMaxY(userPhoto.frame) + 5;
    CGFloat editBtnW = userPhotoW;
    CGFloat editBtnH = 30;
    editBtn.frame = CGRectMake(editBtnX, editBtnY, editBtnW, editBtnH);
    
    /** 个性签名 */
    UILabel *userSignLabel = [[UILabel alloc]init];
    userSignLabel.text = [self.user.info isEqualToString:@""] ? @"这个人好懒啊，什么也没有写" : self.user.info;
    userSignLabel.textColor = [UIColor grayColor];
    userSignLabel.numberOfLines = 0;
    userSignLabel.font = [UIFont systemFontOfSize:14];
    [bgImageView addSubview:userSignLabel];
    
    CGSize userSignLabelSize = [userSignLabel.text sizeWithAttributes:@{NSFontAttributeName:userSignLabel.font}];
    int rows = userSignLabelSize.width / 250 + 1;
    
    CGFloat userSignLabelX = userNameLabelX;
    CGFloat userSignLabelY = editBtnY;
    CGFloat userSignLabelW = 250;
    CGFloat userSignLabelH = rows * userSignLabelSize.height;
    userSignLabel.frame = CGRectMake(userSignLabelX, userSignLabelY, userSignLabelW, userSignLabelH);
    
    /** 上传的视频 */
    UILabel *upLoadLabel = [[UILabel alloc]init];
    upLoadLabel.text = @"我的视频";
    upLoadLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:upLoadLabel];
    
    CGFloat upLoadLabelX = 5;
    CGFloat upLoadLabelY = CGRectGetMaxY(bgImageView.frame) + 5;
    CGFloat upLoadLabelW = self.view.frame.size.width - upLoadLabelX * 2;
    CGFloat upLoadLabelH = 30;
    upLoadLabel.frame  =CGRectMake(upLoadLabelX, upLoadLabelY, upLoadLabelW, upLoadLabelH);
    
    LXMineCellView *upLoadCellView = [LXMineCellView initWithTitle:@"上传的视频" image:@"upLoad"];
    [self.view addSubview:upLoadCellView];
    
    CGFloat upLoadCellViewX = 5;
    CGFloat upLoadCellViewY = CGRectGetMaxY(upLoadLabel.frame) + 5;
    CGFloat upLoadCellViewW = self.view.frame.size.width - upLoadCellViewX * 2;
    CGFloat upLoadCellViewH = 40;
    upLoadCellView.frame = CGRectMake(upLoadCellViewX, upLoadCellViewY, upLoadCellViewW, upLoadCellViewH);
    
    UITapGestureRecognizer *upLoadCellViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(upLoadCellViewTap)];
    [upLoadCellView addGestureRecognizer:upLoadCellViewTap];
}

#pragma mark - 上传视频
- (void)upLoadCellViewTap
{
    if (self.user.allvideos == 0) {
        [MBProgressHUD showError:@"暂无上传的视频"];
    }else{
        // 1.创建请求管理对象
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        NSString *url = @"http://cat666.com/cat666-interface/index.php/index/getVideoById";
        // 2.发送请求
        [mgr POST:url parameters:@{@"userid":@(self.user.ID)} success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                [MBProgressHUD showError:@"暂无上传的视频"];
            }else{
                NSArray *arr = responseObject;
                NSMutableArray *videoArr = [NSMutableArray array];
                for (int i = 0; i < arr.count; i++) {
                    
                }
                for (NSDictionary *dict in responseObject) {
                    LXVideo *video = [LXVideo initWithDict:dict];
                    [videoArr addObject:video];
                }
                self.videoArr = videoArr;
                
                LXUpLoadVideoViewController *upLoadViewController = [[LXUpLoadVideoViewController alloc]init];
                upLoadViewController.videoArr = videoArr;
                [self.navigationController pushViewController:upLoadViewController animated:YES];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [MBProgressHUD showSuccess:@"您的网络开小差了..."];
        }];
    }
}

#pragma mark - 编辑用户信息
- (void)edit
{
    LXEditViewController *editViewController = [[LXEditViewController alloc]init];
    editViewController.user = self.user;
    [self.navigationController pushViewController:editViewController animated:YES];
}

- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
